﻿using IncoolDicomViewerBase;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Windows.Forms;
using xDataSet;
using DICOMViewer.Utility;

namespace IncoolReg
{
    public partial class OfflineForm : MainForm
    {
        protected override void form_init()
        {
            base.form_init();
            //追加窗口构建逻辑
            InitFromlocal();

            this.Text = this.Text + "(离线状态)";
        }


        private void InitFromlocal()
        {
            //获取省区公司数据
            List<Hashtable> comResult = GetProvinceData();
            //初始化省区公司下拉菜单
            if (comResult != null) InitProvinceControl(comResult);
            //获取科室类别数据
            comResult = GetDepartmentData();
            //初始化科室类别下拉菜单
            if (comResult != null) InitDepartmentControl(comResult);
        }

        protected override List<Hashtable> GetDepartmentData()
        {
            return
                SerializeHelper.DeserializeWithJsonFile<List<Hashtable>>(string.Format(OfflineUtility.DataJsonSavePathFormat,
                    "department.json"));
        }

        protected override List<Hashtable> GetProvinceData()
        {
            return
                SerializeHelper.DeserializeWithJsonFile<List<Hashtable>>(string.Format(OfflineUtility.DataJsonSavePathFormat,
                    "zuzhi.json"));
        }

        protected override List<Hashtable> GetHospitalDataByCompanyUuid(string companyUuid)
        {
            List<Hashtable> list = SerializeHelper
                .DeserializeWithJsonFile<List<Hashtable>>(string.Format(OfflineUtility.DataJsonSavePathFormat, "hospital.json"));
            return list.AsEnumerable().Where(o => companyUuid == o["zuzhiId"].ToString()).ToList();
        }

        protected override List<Hashtable> GetOfficeDataByHospital(string hospitaluuid)
        {
            List<Hashtable> list = SerializeHelper
                .DeserializeWithJsonFile<List<Hashtable>>(string.Format(OfflineUtility.DataJsonSavePathFormat, "office.json"));
            return list.AsEnumerable().Where(o => hospitaluuid == o["hospitalId"].ToString()).ToList();
        }

        
        protected override bool SaveEquipmentData(){
            string saveDirPath = string.Format(OfflineUtility.OfflineDataSavePathFormat, string.Empty);
            if (!Directory.Exists(saveDirPath)) Directory.CreateDirectory(saveDirPath);
            Hashtable ht = new Hashtable();
            ht.Add("equipment_id", txt_sn.Text);
            ht.Add("equipment",
                String.IsNullOrEmpty(txt_name.Text.Trim()) == true ? "未命名" : txt_name.Text.Trim());
            ht.Add("province_id", mProvinceId);
            ht.Add("province_name", mProvinceName);
            ht.Add("city_id", mCityId);
            ht.Add("city_name", mCityName);
            ht.Add("area_id", mAreaId);
            ht.Add("area_name", mAreaName);
            ht.Add("zuzhi_id", mZuzhiId);
            ht.Add("zuzhi_name", mZuzhiName);
            ht.Add("department_category_id", mDepartmentId);
            ht.Add("department_category_name", mDepartmentName);
            ht.Add("hospitail_id", mHospitailId);
            ht.Add("hospitail_name", mHospitailName);
            ht.Add("office_id", mOfficeId);
            ht.Add("office_name", mOfficeName);
            SerializeHelper.SerializeToJsonFile(ht, string.Format(OfflineUtility.OfflineDataSavePathFormat, mMacAddress+".json"));
            return true;
        }
    }
}